package com.group15.phms;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Registration2 extends Activity {
	private List<EditText> nameList = new ArrayList<EditText>();
	private List<EditText> emailList = new ArrayList<EditText>();
	private ArrayList<String> names = new ArrayList<String>();
	private ArrayList<String> emails = new ArrayList<String>();
	private int count=0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registration2);
		
		findViewById(R.id.nextButton).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						nextPage();
					}
				});
		findViewById(R.id.cancelButton).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						cancel();
					}
				});
		findViewById(R.id.addDoctors).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						createNew();
					}
				});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.registration2, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.addbutton:
			createNew();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	//Store values and move to next page
	public void nextPage() {
		EditText e,n;
		boolean cancel = false;
		if(nameList.isEmpty()) //if no fields have been added
		{
			setResult(1);
			finish();
		}
		//if one field of a pair is filled in, prompt to fill in the missing one
		//if the last pair of fields is empty, ignore
		else
		{
			for (int i=0; i<nameList.size()-1; i++ )
			{
				//System.out.println(nameList.get(i).getText().toString());
				//System.out.println(emailList.get(i).getText().toString());
				
				n=nameList.get(i);
				n.setError(null);
				if (TextUtils.isEmpty(n.toString()))
				{
					n.setError(getString(R.string.error_field_required));
					cancel=true;
				}
				e=emailList.get(i);
				e.setError(null);
				if (TextUtils.isEmpty(e.toString()))
				{
					e.setError(getString(R.string.error_field_required));
					cancel=true;
				}
				
			}
			//test last pair of form fields
			int i=nameList.size()-1;
			n=nameList.get(i);
			e=emailList.get(i);
			if (TextUtils.isEmpty(n.toString())&&TextUtils.isEmpty(e.toString()))
				e.setError(null);
			if (TextUtils.isEmpty(n.toString())&&!TextUtils.isEmpty(e.toString()))
			{
				n.setError(getString(R.string.error_field_required));
				cancel=true;
			}
			if (!TextUtils.isEmpty(n.toString())&&TextUtils.isEmpty(e.toString()))
			{
				e.setError(getString(R.string.error_field_required));
				cancel=true;
			}

			if (cancel) {
				// There was an error; don't attempt login and focus the first form field with an error.
				//focusView.requestFocus();
			} 
			else {
				// go to next page
				for (i=0; i<nameList.size(); i++ )
				{
					names.add(nameList.get(i).getText().toString());
					emails.add(emailList.get(i).getText().toString());
				}
				Bundle b = new Bundle();
				b.putStringArrayList("names", names);
				b.putStringArrayList("emails", emails);
				Intent result = new Intent();
				result.putExtras(b);
			    
				setResult(2,result);
				finish();
			}
		}
	}
	
	public void cancel() {
		setResult(RESULT_CANCELED);
		finish();
	}
	
	
	
	
	
	//helper function to set horizontal rule margins
	public static void setMargins (View v, int l, int t, int r, int b) {
	    if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
	        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
	        p.setMargins(l, t, r, b);
	        v.requestLayout();
	    }
	}
	
	//create a new form field
	public void createNew()
	{
		LinearLayout ll = (LinearLayout)findViewById(R.id.mainLayout);
		int index = ll.indexOfChild(findViewById(R.id.addDoctors));
		
		if (count==0)
			index= index-1;
		
		if (count>0)
		{
			View ruler = new View(this);
			LinearLayout.LayoutParams viewLp=new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 10);
			viewLp.setMargins(0,20,0,15);//left,top,right,bottom
			ruler.setLayoutParams(viewLp);
			ruler.setBackgroundColor(Color.parseColor("#0099CC"));
			ll.addView(ruler,index, viewLp);
		}
		
		TextView textName = new TextView(this);
        textName.setText("Name");
        ll.addView(textName,index+1);
        
        EditText nameField=new EditText(this);
        nameField.setTextColor(Color.BLACK);
        nameField.setBackgroundResource(R.drawable.edittext_modified_states);
        nameField.setHint(R.string.first_last);
        nameField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS | InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
        nameField.requestFocus();
        try {
	        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
	        mCursorDrawableRes.setAccessible(true);
	        mCursorDrawableRes.set(nameField, 0); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
	    } catch (Exception e) {}
        ll.addView(nameField,index+2);
        nameList.add(nameField);
        
        TextView textEmail = new TextView(this);
        textEmail.setText("Email Address");
        ll.addView(textEmail,index+3);
        
        EditText emailField=new EditText(this);
        emailField.setTextColor(Color.BLACK);
        emailField.setBackgroundResource(R.drawable.edittext_modified_states);
        emailField.setHint(R.string.text_email);
        emailField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        try {
	        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
	        mCursorDrawableRes.setAccessible(true);
	        mCursorDrawableRes.set(emailField, 0); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
	    } catch (Exception e) {}
        ll.addView(emailField,index+4);
        emailList.add(emailField);
        count++;
	}//end createNew
	
}

